const app = getApp();
let ServiceUrl = app.globalData.ServiceUrl;
var dateTimePicker = require('../../utils/dateTimePicker.js');
var QQMapWX = require('../../utils/qqmap-wx-jssdk.js');
var qqmapsdk = new QQMapWX({
  key: '66XBZ-6EWK2-YZ7UP-CPLDJ-XQLUK-TTFJ6'
})
Page({
  /**
   * 页面的初始数据
   */
  data: {
    selectPeople: '', //就诊对象姓名
    selectPeopleId: '',  //就诊对象id
    selectHospital: '',  //就诊医院 配药药店  配药诊所
    latitude:'',//出发地点经度
    longitude:'',//出发地点纬度
    address:'',  //出发地址
    selectHospitalId: '',//医院ID
    radio: '',//选择出行方式 1：公共交通 2：出租车 3：步行 4：非急救转运
    require_date: '',
    require_time: '',
    typeid: '',//服务类型
    user_id: '',
    doorplate: '',

    serviceName:'',
    dateTime1: null,//时间选择器
    dateTimeArray1: null,
    acceptRule: false,//协议
    show: false,
    showXieyi: false,
    radioSelect: '1',
    isdpy: false, //是否代配药界面
    is_hospital: 0,//代配药是医院还是药店
    disease:'',//所患疾病
    symptom:'',//目前症状
    others:'',//其他诉求
  },
  //选择代配药医院还是药店
  radioChange(event) {
    console.log(event.detail)
    if(event.detail == 1){
      var selectHospital = '选择代配药医院';
      var is_hospital = 0;
    }else{
      var selectHospital = '选择代配药药店';
      var is_hospital = 1;
    }
    this.setData({
      radioSelect: event.detail,
      selectHospital: selectHospital,
      is_hospital: is_hospital
    });
  },
  doorChange(event) {
    // event.detail 为当前输入的值
    this.setData({doorplate:event.detail})
  },
  familiar: function()
  {
    this.showModal('您还没有熟悉的陪诊人员')
  },
  //报错封装
  showModal(error){
    wx.showModal({
      content:error,
      showCancel: false,
      confirmColor: 'skyblue',
    })
  },
  //所患疾病
  diseaseChange(event) {
    // event.detail 为当前输入的值
    this.setData({disease: event.detail})
  },
  //目前症状
  symptomChange(event) {
    // event.detail 为当前输入的值
    this.setData({symptom: event.detail})
  },
  //其他诉求
  othersChange(event) {
    // event.detail 为当前输入的值
    this.setData({others: event.detail})
  },
  //提交订单信息
  submit: function(){
    let that = this;
    let typeid = that.data.typeid;
    console.log(typeid)
    if(that.data.selectPeopleId == ''){
      that.showModal('请先选择就诊对象');
      return false;
    }
    if(that.data.selectHospitalId == '' && that.data.typeid == 3){
      that.showModal('请先选择代配药医院或药店')
      return false;
    }else if(that.data.selectHospitalId == ''){
      that.showModal('请先选择就诊医院')
      return false;
    }
    if(that.data.typeid == 3){
      if(that.data.disease == ''){
        that.showModal('请填写所患疾病信息')
        return false;
      }
      if(that.data.symptom == ''){
        that.showModal('请填写所目前症状')
        return false;
      }
    }
    if(that.data.latitude == ''){
      that.showModal('请先刷新页面或者重新进入页面获取地理位置')
      return false;
    }
    if(that.data.require_date == ''){
      that.showModal('出发时间不可为当前时间');
      return false;
    }else{
      let that = this
      var newDate = new Date();
      //console.log(newDate.getDate())
      //将当前时间加上一定的时间作比较，不能低于某个时间段,出发时间应大于现在的二小时后
      let nowDate = newDate.getDate(); //今天几号
      let selectDate = parseInt(that.data.dateTimeArray1[2][that.data.dateTime1[2]]);  //选择几号
      let hour = newDate.getHours();//当前几点
      let selectHour = parseInt(that.data.dateTimeArray1[3][that.data.dateTime1[3]]);  //选择几点
      //如果选择的时间是今天之前的日期就提示
      if(selectDate < nowDate){
        //console.log(selectDate)
        that.showModal('请选择好预约时间');
        return false;
      }
      if(nowDate == selectDate){
        if(hour + 2 > selectHour){
          that.showModal('请预约于当前2小时之后的时间');
          return false;
        }
      }
    }
    if(that.data.doorplate == ''){
      that.showModal('请输入详细地址')
      return false;
    }
    if(that.data.radio == ''){
      that.showModal('请先选择出行方式')
      return false;
    }
    if(that.data.acceptRule == false){
      that.showModal('请先同意服务协议')
      return false;
    }
    console.log(that.data)
    wx.request({
      url: ServiceUrl + 'accompanyAdd',
      data: {
        typeid: typeid,//服务类型
        oldman_id: that.data.selectPeopleId,  //就诊对象id
        hospital_id: that.data.selectHospitalId,//医院ID
        start_latitude: that.data.latitude,//出发地点经度
        start_longitude: that.data.longitude,//出发地点纬度
        start_name: that.data.address,  //出发地址
        require_transport: that.data.radio,//选择出行方式 1：公共交通 2：出租车 3：步行 4：非急救转运
        require_date: that.data.require_date,
        require_time: that.data.require_time,
        user_id: that.data.user_id,
        doorplate: that.data.doorplate,
        disease: that.data.disease,
        symptom: that.data.symptom,
        others: that.data.others,
        is_hospital: that.data.is_hospital
      },
      method: "get",
      success: function(res){
        //console.log(res)
        if(res.data.state == 200){
          wx.showLoading({
            title: res.data.msg,
          })
          setTimeout(function () {
            wx.hideLoading();
            wx.navigateTo({
              url: '../order/submitOrder?order_id='+res.data.order_id + '&user_id='+that.data.user_id,
            })
          },2000)       
          
        }
      }
    })
  },
  //同意协议
  agreement:function(){
    this.setData({ showXieyi: true });
  },
  onClose:function() {
    this.setData({ showXieyi: false });
  },
  //接受服务协议
  bindAcceptRule: function(e) {
    let that = this
    // 同意规则
//    console.log('picker发送选择改变，携带值为', e.detail.value)
      if (e.detail.value[0] == "同意") {
        that.data.acceptRule = true
      }else{
        that.data.acceptRule = false
      }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    var that = this
    if(options.typeid == 1){
      var serviceName = '陪伴门诊';
      var people = '选择就诊对象';
      var selectHospital = '选择就诊医院';
      var isdpy = false;//不是代配药？ 
    }else if(options.typeid == 2){
      var serviceName = '陪伴血透';
      var people = '选择就诊对象';
      var selectHospital = '选择就诊医院';
      var isdpy = false;  //不是代配药？
    }else if(options.typeid == 3){
      var serviceName = '代配药';
      var people = '选择代配药对象';
      var selectHospital = '选择代配药医院';
      var isdpy = true; //是代配药
    }
    if(that.data.selectPeople){
      var people = that.data.selectPeople
    }
    that.postation();
    // 获取完整的年月日 时分秒，以及默认显示的数组
     var obj1 = dateTimePicker.dateTimePicker(that.data.startYear, that.data.endYear);
     // 精确到分的处理，将数组的秒去掉
     var lastArray = obj1.dateTimeArray.pop();
     var lastTime = obj1.dateTime.pop();
      let user_id = options.user_id
      that.setData({
         dateTimeArray1: obj1.dateTimeArray,
         dateTime1: obj1.dateTime,
         thisTimeArray: obj1.dateTimeArray,
         thisTime: obj1.dateTime,
         user_id: user_id,
         typeid: options.typeid,
         serviceName: serviceName,
         selectHospital: selectHospital,
         selectPeople: people,
         isdpy : isdpy
      });
      
  },
  changeDateTime1(e) {
         this.setData({ dateTime1: e.detail.value });
  },
  changeDateTimeColumn1(e) {
         var arr = this.data.dateTime1, dateArr = this.data.dateTimeArray1;
         arr[e.detail.column] = e.detail.value;
         dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);
         this.setData({ 
           dateTimeArray1: dateArr,
           dateTime1: arr,
           require_date: dateArr[0][arr[0]] + '-' + dateArr[1][arr[1]] + '-' + dateArr[2][arr[2]],
           require_time: dateArr[3][arr[3]] + ':' + dateArr[4][arr[4]] + ':' + '00'
         });
//         console.log(this.data.thisTimeArray[0][this.data.thisTime[0]])
  },
  /**
   * 获取当前经纬度
   */
  postation:function(){
    let that = this
    //获取当前的地理位置、速度API
    wx.getLocation({
      type: 'gcj02',
      success (res) {
        console.log(res)
        if(res.errMsg=="getLocation:ok"){
          that.setData({
            latitude: res.latitude,
            longitude:res.longitude
          });
          that.qqmap();
          //console.log('获取定位成功')
        }else{
          console.log('获取定位失败')
        }
      }
     });
  },
  /**
   * 腾讯地图逆地址解析API
   */
  qqmap:function(){
    let that = this
    qqmapsdk.reverseGeocoder({
    //位置坐标，默认获取当前位置，Object格式
    location: {
      latitude: that.data.latitude,
      longitude:that.data.longitude,
    },
    success: function(res) {//成功后的回调
      console.log(res.result)
        that.setData({address:res.result.formatted_addresses.recommend})
        },
    fail: function(error) {
      console.error(error);
   },
    complete: function(res) {
      console.log(res);
    }
  })
  },
/**
 * 选择就诊对象页面
 */
  peopleChange: function(){
    let that = this
    wx.navigateTo({
      url: '../information/people?user_id=' + that.data.user_id + '&typeid=' + that.data.typeid,
    })
  },
  /**
   * 更改定位页面
   */
  addressChange:function() {
    wx.navigateTo({
      url: '../information/map?typeid=' + this.data.typeid,
    })
  },
  /**
   * 选择就诊医院页面
   */
  hospitalChange: function(){
    wx.navigateTo({
      url: '../information/hospital?typeid=' + this.data.typeid,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  //console.log(this.data.selectHospitalId)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
//出行方式事件监听
  tripWay: function () {
    this.setData({ show: true });
  },
//关闭出行方式事件监听
  goWayClose() {
    this.setData({ show: false });
  },
//确认选择出行方式
  onChange(event) {
    this.setData({
      radio: event.detail,
    });
  },
})